﻿using Furion.EventBus;
using KseCms.Core;
using System.Xml.Linq;

namespace KseCms.Application
{
    public class PaymentService : IPaymentService, ITransient
    {
        private readonly IRepository<Payment> _paymentRepository;

        private readonly IEventPublisher _eventPublisher;

        public PaymentService(IRepository<Payment> paymentRepository, IEventPublisher eventPublisher)
        {
            _paymentRepository = paymentRepository;
            _eventPublisher = eventPublisher;
        }

        public async Task Create(PaymentDto payment)
        {
            var entity = payment.Adapt<Payment>();

            await _paymentRepository.InsertNowAsync(entity);

            var payWay=new PayMethodFactory(payment).CreatInstance();

            payWay.PayProcess();

            await PaySucceed(payment);
        }

        public async Task PaySucceed(PaymentDto payment)
        {
            await _eventPublisher.PublishAsync(EventType.PaySuccess, payment);
        }

        public void PayFailed(PaymentDto payment)
        {
            //to-do
        }

    }
}